home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / read_srf.pro < prev    next >
Text File  |  1997-07-08  |  5KB  |  169 lines

  1. ; $Id: read_srf.pro,v 1.5 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1987-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5.  
  6. PRO READ_SRF, FILE, IMAGE, R, G, B
  7. ;
  8. ;+
  9. ; NAME:
  10. ;    READ_SRF
  11. ;
  12. ; PURPOSE:
  13. ;    Read the contents of a Sun rasterfile and return the image and
  14. ;    color table vectors (if present) in the form of IDL variables.
  15. ;
  16. ; CATEGORY:
  17. ;    Input/Output.
  18. ;
  19. ; CALLING SEQUENCE:
  20. ;    READ_SRF, File, Image [, R, G, B]
  21. ;
  22. ; INPUTS:
  23. ;    File:    Scalar string giving the name of the rasterfile to read
  24. ;
  25. ; OUTPUTS:
  26. ;    Image:    The 2D byte array to contain the image.
  27. ;
  28. ;
  29. ; OPTIONAL OUTPUT PARAMETERS:
  30. ;     R, G, B:    The variables to contain the Red, Green, and Blue color vectors
  31. ;        if the rasterfile containes colormaps.
  32. ;
  33. ; COMMON BLOCKS:
  34. ;    None.
  35. ;
  36. ; SIDE EFFECTS:
  37. ;    None.
  38. ;
  39. ; RESTRICTIONS:
  40. ;    This routine only handles 1, 8, 24, and 32-bit rasterfiles of type
  41. ;    RT_OLD and RT_STANDARD.  See the file /usr/include/rasterfile.h
  42. ;    for the structure of Sun rasterfiles.
  43. ;
  44. ; EXAMPLE:
  45. ;    To open and read the Sun rasterfile named "sun.srf" in the current
  46. ;    directory, store the image in the variable IMAGE1, and store the color
  47. ;    vectors in the variables R, G, and B, enter:
  48. ;
  49. ;        READ_SRF, "sun.srf", IMAGE1, R, G, B
  50. ;
  51. ;    To load the new color table and display the image, enter:
  52. ;
  53. ;        TVLCT, R, G, B
  54. ;        TV, IMAGE1
  55. ; MODIFICATION HISTORY:
  56. ;    Written fall 1987, AB
  57. ;    3/1/90, Added 24 bit images, DMS.
  58. ;    7/8/90, Added 32 bit images, DMS.
  59. ;    1/22/92, the colors within 24 bit images were not ordered
  60. ;        correctly, DMS.
  61. ;    5/7/96, Corrected bug in the color vector order for 24 bit
  62. ;        images. As per the "Encyclopedia of Graphics File
  63. ;        formats", O'Rielly & Ass., 24 bit pixel data is in
  64. ;        BGR format rather that RGB. This was verified thru
  65. ;        the use of XV. Also the 32 big reading section did
  66. ;        perform the color correction correctly. kdb.
  67. ;-
  68. ; Copyright (c) 1990, Research Systems, Inc.  All rights reserved.
  69. ;    Unauthorized reproduction prohibited.
  70. ;
  71.  
  72. ; Define the Sun Raster File structure
  73. a = {rasterfile, magic:0L, width:0L, height:0L, depth: 0L, $
  74.     length:0L, type:0L, maptype:0L, maplength:0L}
  75.  
  76. OPENR, unit, file, /GET_LUN, /BLOCK
  77.  
  78. READU, unit, a
  79. ; Check the magic number
  80. if (a.magic EQ '956aa659'XL) then begin
  81.     byteorder, a, /ntohl        ;Back to our order
  82.     endif
  83.     
  84. IF (a.magic NE '59a66a95'X) THEN begin
  85.     message, /CONTINUE, 'File ' + file + $
  86.         ' does not have a SUN rasterfile magic number.'
  87.     GOTO, done
  88.     ENDIF
  89.  
  90. ; Only know how to do RT_OLD and RT_STANDARD style rasterfiles.
  91. IF ((a.type NE 0) AND (a.type NE 1)) THEN BEGIN
  92.     message, /CONTINUE, $
  93.         'Don''t know how to handle this style of rasterfile, sorry...'
  94.     GOTO, done
  95.     ENDIF
  96.  
  97. ; Only know how to handle 1 bit and 8 bit images
  98. IF (a.depth NE 1) AND (a.depth NE 8) AND (a.depth ne 24) AND $
  99.    (a.depth ne 32) then begin
  100.     message, /CONTINUE, 'Can''t handle ' + string(a.depth) + ' bit images.'
  101.     GOTO, done
  102.     ENDIF
  103.  
  104. ; Recompute length, since rasterfile.h says you have to for old files...
  105. a.length = (a.width * a.height * a.depth) / 8
  106. image = 0        ;Delete original value of image var
  107.  
  108. ; If image has color tables, read them
  109. IF ( (a.maptype EQ 1) AND (a.maplength NE 0) ) THEN BEGIN
  110.     maplen = a.maplength / 3
  111.     r = BYTARR(maplen)
  112.     g = BYTARR(maplen)
  113.     b = BYTARR(maplen)
  114.     READU, unit, r, g, b
  115.     ENDIF
  116.  
  117. ; Get a byte array for the image
  118.  
  119. ; If the image is 1 bit, read it and decode into bytes
  120. case a.depth of
  121. 1: begin
  122.     ncols = ((a.width + 15)/16)*16  ;# of columns padded to short
  123.     image = BYTARR(ncols, a.height) ;make the array
  124.     temp = intarr(a.length/2)    ; Get the packed shorts
  125.     READU, unit, temp
  126.         mask = ROTATE((2 ^ indgen(16)),2) ;Array of 16 bits
  127.     FOR i = 0,15 DO BEGIN        ;Unpack each bit
  128.         ind = WHERE(temp AND mask[i])
  129.         s = size(ind)
  130.         if (s[0] ne 0) THEN image[ind * 16 + i] = 255
  131.         ENDFOR
  132. ENDCASE
  133.  
  134. 8: BEGIN    ; If the image is eight bit, read it
  135.     ncols = ((a.width + 1)/2)*2  ;Pad to even number
  136.     image = BYTARR(ncols, a.height)
  137.     READU, unit, image
  138. ENDCASE
  139.  
  140. 24: BEGIN        ;24 bit
  141.     ncols = ((3*a.width + 1)/2)*2  ;Pad to even number of bytes
  142.     image = BYTARR(3, a.width, a.height)
  143.     ; Bug fix. RGB vectors are store BGR in standard SRF!
  144.  
  145.     row = bytarr(ncols); read a row at a time.
  146.     indices = [2,1,0] # replicate(1,a.width*3) + $
  147.         (3 * (indgen(a.width*3)/3))
  148.     for i=0, a.height-1 do begin
  149.         readu, unit, row    
  150.         image[0,0,i] = reform(row[indices], 3, a.width, /over)
  151.     endfor
  152. ENDCASE
  153.  
  154. 32:    BEGIN        ;Read 32 bit into a (3,n,m) image
  155.     image = BYTARR(3,a.width, a.height)
  156.     row = bytarr(4,a.width)    ;Read a row at a time
  157.     indices = [3,2,1] # replicate(1,a.width*3) + $
  158.         (4 * (indgen(a.width*3)/3))
  159.     for i=0,a.height-1 do begin
  160.         readu, unit, row
  161.         image[0,0,i] = reform(row[indices],3,a.width,/over)
  162.         endfor
  163. ENDCASE
  164. ENDCASE
  165. done:
  166.     FREE_LUN, unit
  167.     END
  168.